package com.tangl.demo.rabbitMQ;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.tangl.demo.Document.LogDocument;
import com.tangl.demo.service.logToMongoService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @author: TangLiang
 * @date: 2020/8/3 16:26
 * @since: 1.0
 */
@Component
public class LogToMongoMQ {
    @Autowired
    private logToMongoService logToMongoService;

    //监听logToMongo队列
    @RabbitListener(queuesToDeclare = @Queue("logToMongo"))
//    @RabbitListener(bindings=@QueueBinding(
//            value=@Queue("mysolamsg"),
//            exchange=@Exchange("myexchange"),
//            key="myso"
//    ))
    public void createLog(Message message, Channel channel) throws UnsupportedEncodingException {
        //System.out.println(channel);
        String msg = new String(message.getBody(), "utf-8");
        LogDocument logDocument = JSON.parseObject(msg, LogDocument.class);
        logToMongoService.create(logDocument);
    }
}
